OR Condition

Course- PostgreSQL >

This PostgreSQL tutorial explains how to use the PostgreSQL OR condition with syntax and examples.

Description

The PostgreSQL OR condition is used to test two or more conditions where records are returned when any one of the conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the OR condition in PostgreSQL is:

WHERE condition1

OR condition2

...

OR condition_n;

Parameters or Arguments

condition1, condition2, condition_n are any of the conditions that must be met for the records to be selected.

Note

  • The PostgreSQL OR condition allows you to test 2 or more conditions.
  • The PostgreSQL OR condition requires that any of the conditions (ie: condition1, condition2, condition_n) be must be met for the record to be included in the result set.

Example - With SELECT Statement

The first PostgreSQL OR condition example that we'll take a look at involves a SELECT statement with 2 conditions:

SELECT *

FROM products

WHERE product_type = 'Hardware'

OR product_id > 400;

This PostgreSQL OR condition example would return all products whose product_type is 'Hardware' or have a product_id greater than 400. Because the * is used in the SELECT statement, all fields from the products table would appear in the result set.

Example - With SELECT Statement (3 conditions)

The next PostgreSQL OR example looks at a SELECT statement with 3 conditions. If any of these conditions is met, the record will be included in the result set.

SELECT product_id, product_name

FROM products

WHERE product_type = 'Hardware'

OR product_type = 'Software'

OR product_id > 1000;

This PostgreSQL OR condition example would return all product_id and product_name values from the products table where the product_type is 'Hardware', the product_type is 'Software', r the product_id is greater than 1000.

Example - With INSERT Statement

The PostgreSQL OR condition can be used in the INSERT statement.

For example:

INSERT INTO products

(product_id, product_name)

SELECT inventory_id, product_name

FROM inventory

WHERE quantity > 0

OR product_name = 'Memory';

This PostgreSQL OR example would insert into the products table, all inventory_id and product_name records from the inventory table whose quantity is greater than 0 or product_name is 'Memory'.

Example - With UPDATE Statement

The PostgreSQL OR condition can be used in the UPDATE statement.

For example:

UPDATE products

SET product_type = 'Hardware'

WHERE product_name = 'Memory'

OR product_name = 'SSD';

This PostgreSQL OR condition example would update all product_type values in the products table to 'Hardware' where the product_name is 'Memory' or the product_name is 'SSD'.

Example - With DELETE Statement

The PostgreSQL OR condition can be used in the DELETE statement.

For example:

DELETE FROM contacts

WHERE last_name = 'Anderson'

OR first_name = 'Sarah';

This PostgreSQL OR condition example would delete all contacts from the contacts table whose last_name was 'Anderson' or first_name was 'Sarah'.